home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
pc
/
mac_file
/
vendor_d
/
neuralwa
/
nw2v50
/
artio.c
< prev
next >
Wrap
Text File
|
1993-08-23
|
42KB
|
1,323 lines
/* artio.c - character recognition user I/O function
/************************************************************************
* Copyright(C) 1987-1992 NeuralWare Inc *
* Penn Center West, IV-227, Pittsburgh, PA 15276 *
* Telephone: (412) 787-8222 FAX: (412) 787-8220 *
* *
* All rights reserved. No part of this program may be reproduced, *
* stored in a retrieval system, or transmitted, in any form or by any *
* means, electronic, mechanical, photocopying, recording or otherwise *
* without the prior written permission of the copyright owner, *
* NeuralWare, Inc. *
* *
* PROPRIETARY NOTICE *
* *
* This document is the property of NeuralWare, Inc. and contains *
* trade-secrets and other proprietary information. The information *
* herein is reserved as proprietary to NeuralWare, and is not to be *
* published, reproduced, copied, disclosed, used, or reverse *
* engineered without the express written consent of a duly authorized *
* representative of NeuralWare. *
************************************************************************
*/
#ifndef SKY
#include <string.h>
#endif
#define UIO_SERVER 1
#define SERVER_EMULATOR_FILE 1
#define SKIP_COMPAT_DATA 1
#include "userutl.h"
#include "uio_strc.h"
#include "uio_tran.h"
#include "uio_reqs.pro"
#include "uio_cmds.pro"
#ifndef SUN
#ifndef DLC
#include <stdlib.h>
#endif
#endif
#ifdef MAC
#include "macuio.redef"
#endif
/************************************************************************
* *
* Character Recognition User I/O Routine *
* *
* Options: *
* *
* ATTENTION I/O: This allows the user to create a file of *
* character patterns and their associated *
* ASCII code. This file is written in .nna *
* format and can be used either for File I/O *
* or for User I/O learning. The user *
* is prompted for file name (.nna extension *
* assumed), and is given the option of writing or *
* appending the file. The user is provided with *
* a grid in which to draw patterns. On entering *
* a pattern, the user is prompted to enter, from *
* the keyboard, the character that the pattern *
* depicts. The file is closed on quitting. *
* *
* REWIND: If a file is open it is rewound. *
* *
************************************************************************
*/
/* grid definitions */
static long n_across, n_down, n_inputs;
#define N_ACR_MIN 1
#define N_ACR_MAX 16
#define N_ACC_DEF 6
#define N_DWN_MIN 1
#define N_DWN_MAX 16
#define N_DWN_DEF 8
/* window coordinates - initialized in usrIO( ) */
static int wxstrt[4], wystrt[4], wxend[4], wyend[4];
typedef struct _grid {
unsigned char gr_key; /* window key for grid */
unsigned char gr_flag; /* grid flag */
int gr_xoff, gr_yoff; /* x,y offset from window origin */
int gr_nx, gr_ny; /* # pixels in x, y directions */
int gr_pxx, gr_pxy; /* size of a grid pixel */
int gr_color[3]; /* color of lines and pixels */
#define GR_OFF 0 /* off color index */
#define GR_ON 1 /* on color index */
#define GR_LINE 2 /* line color index */
float *gr_data; /* data for grid */
} GRID;
static GRID ip_grid = { 0 }; /* Input Grid */
static GRID f1_grid = { 0 }; /* F1 grid (ART 1) */
static float f1data[ N_ACR_MAX * N_DWN_MAX ]; /* f1 data */
static char ip_text[] = "Input Pattern";
static char f1_text[] = "F1 Activity";
static int wrstepf = 0 ; /* WRSTEP flag */
/* Local menu definitions */
#define CR_SCROLL 0
#define CR_SEEK 1
#define CR_ENTER 2
#define CR_QUIT 3
#define CR_AUTO 4
#define NULL_STR 0
static GMENU_ITEM PMenuList[] = { /* Menu list */
{ CR_SCROLL, NULL_STR, NULL_STR },
{ CR_SEEK, NULL_STR, NULL_STR },
{ CR_ENTER, NULL_STR, NULL_STR },
{ CR_QUIT, NULL_STR, NULL_STR },
{ CR_AUTO, NULL_STR, NULL_STR }
};
static GMENU AIOMenu = { /* Attention I/O menu */
0,
4, /* # items */
3, /* key */
0x0000
};
static GMENU LrnMenu = { /* Attention I/O menu */
0,
5, /* # items */
3, /* key */
0x0000
};
/* File definitions */
static char filename[13];
static char filenroot[9] = "art1"; /* Default .nna file */
static int curr_pattern, n_patterns; /* pattern index, # patterns */
#define MAX_N_PATTERNS 100
static long filepos[ MAX_N_PATTERNS ]; /* Array of file positions */
static int shuffle_array[ MAX_N_PATTERNS ];/* Index into shuffle array */
static int shufflex = 0 ; /* Shuffle Index */
#define MAX_STR 78 /* For reading from file */
static char fbuf[MAX_STR + 1]; /* buffer for file i/o */
static FILE *fp = 0; /* File pointer */
static float fdata[ N_ACR_MAX * N_DWN_MAX ]; /* file data */
static float fout[7]; /* file desired output */
/* Network parameters */
static int nlayp, ninp, noutp, ltype; /* Network parameters */
static char *csp, *netnp; /* Network pointers*/
/* Graphics parameters */
static int xsize, ysize, ncolor, chrx, chry; /* graphics parameters */
/* Miscellaneous */
static int abortio;
static int autoip;
#ifdef DLC
/* --- prototypes --- */
DispGrid( GRID * );
DispPattern( GRID * );
Sketch( GRID * );
DrawPixel( GRID *, int, int, int );
FillPosArr( );
ReadFData( );
WriteFData( );
Shuffle( int );
#endif
DispGrid( gridp )
GRID *gridp; /* pointer to grid structure */
{
int wx; /* work index */
int x0,y0,x1,y1; /* endpoints of gridlines */
/* Draw vertical gridlines */
x1 = x0 = gridp->gr_xoff;
y0 = gridp->gr_yoff;
y1 = y0 + gridp->gr_ny * ( gridp->gr_pxy + 1 );
for (wx = 0; wx <= gridp->gr_nx; wx++, x1 = x0 += gridp->gr_pxx + 1 )
ug_line( gridp->gr_key, gridp->gr_color[GR_LINE], 0, x0, y0, x1, y1, 0 );
/* Draw horizontal gridlines */
x0 = gridp->gr_xoff;
x1 = x0 + gridp->gr_nx * ( gridp->gr_pxx + 1 );
y1 = y0 = gridp->gr_yoff;
for (wx = 0; wx <= gridp->gr_ny; wx++, y1 = y0 += gridp->gr_pxy + 1 )
ug_line( gridp->gr_key, gridp->gr_color[GR_LINE], 0, x0, y0, x1, y1, 0 );
}
VOID DispPattern( gridp )
GRID *gridp; /* Pointer to grid structure */
{
int wx, wy, wz; /* work indices */
float *dp; /* pointer to data */
dp = &gridp->gr_data[0];
if ( dp == ( float * )0 ) return;
for ( wy = gridp->gr_ny, wz = 0; --wy >= 0 ; )
for ( wx = 0; wx < gridp->gr_nx; wx++, wz++ )
DrawPixel( gridp, wx, wy,
gridp->gr_color[ dp[wz]>0.0001 ? GR_ON : GR_OFF ] );
}
VOID Sketch( gridp )
GRID *gridp; /* Pointer to grid structure */
{
int x0, y0, x1, y1; /* Grid boundary */
int wx, wy, wz; /* work indices */
int key, xp, yp, button; /* mouse parameters */
float *dp; /* pointer to data */
dp = &gridp->gr_data[0];
if ( dp == ( float * )0 ) return;
x0 = gridp->gr_xoff;
y0 = gridp->gr_yoff;
x1 = x0 + gridp->gr_nx * ( gridp->gr_pxx + 1 );
y1 = y0 + gridp->gr_ny * ( gridp->gr_pxy + 1 );
for (;;) {
ug_mouse( &key, &xp, &yp, &button );
if ( key != gridp->gr_key || xp < x0 || xp >= x1 || yp < y0 || yp >= y1 )
return;
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
/* Calculate grid coordinate */
wx = ( xp - x0 ) / ( gridp->gr_pxx + 1 );
wy = ( yp - y0 ) / ( gridp->gr_pxy + 1 );
DrawPixel( gridp, wx, wy,
gridp->gr_color[ button == MBUT_LEFT ? GR_ON : GR_OFF ] );
/* Modify data array */
wz = (gridp->gr_ny - wy - 1) * gridp->gr_nx + wx;
dp[wz] = button == MBUT_LEFT ? 1.0 : -1.0;
}
}
}
DrawPixel( gridp, x, y, color )
GRID *gridp; /* pointer to grid structure */
int x, y; /* grid coordinate */
int color; /* pixel color */
{
int xstrt, ystrt;
xstrt = gridp->gr_xoff + 1 + ( gridp->gr_pxx + 1 ) * x;
ystrt = gridp->gr_yoff + 1 + ( gridp->gr_pxy + 1 ) * y;
ug_boxf( gridp->gr_key, color, 0, xstrt, ystrt,
xstrt + gridp->gr_pxx - 1, ystrt + gridp->gr_pxy - 1 );
}
/* NOTE: following routine will not cope with
general format ".nna" files. In particular, it
is assumed that any new line which does not
start with a continuation character ('&') or
a comment character ('!') is the start of a
non-empty record; it is also assumed that there
are no comments in the middle of records */
FillPosArr( )
{
int wx; /* work index */
int ch; /* Character */
long fpos; /* File position */
int ftt; /* First time through */
fseek( fp, 0l, 0 ); /* beginning of file */
for ( wx = 0; wx < MAX_N_PATTERNS; ) {
if ( (ch=fgetc( fp )) != '!' && ch != '&' ) {
ungetc( ch, fp );
fpos = ftell( fp );
ftt = 1;
} else ftt = 0;
/* Make sure there is something on the line;
scan to end of line */
while ( (ch=fgetc( fp)) != NEW_LINE ) {
if ( ftt && ch >= (int)'0' ) {
filepos[wx++] = fpos;
ftt = 0;
}
if ( ch == EOF ) return( wx );
}
}
}
ReadFData( flag )
int flag; /* Whether or not desired output should be read */
{
int wx, wy; /* work index */
int ch;
float *dp; /* data pointer */
char sbuf[100];
if ( curr_pattern < 0 ) curr_pattern = 0;
if ( curr_pattern >= n_patterns ) { /* Don't read from file */
for ( wx = 0; wx < n_inputs; wx++ )
fdata[wx] = -1.0;
curr_pattern = n_patterns;
return( 0 );
} else {
fseek( fp, filepos[curr_pattern], 0 );
/* NOTE: Following code assumes data has been written using
format of WriteFData( ) routine */
for (wx = 0, wy = 18, dp = fdata; wx < n_inputs;
wx++, wy--, dp++ ) {
if ( wy == 0 ) { /* Look for continuation */
while ( (ch=fgetc( fp)) != NEW_LINE )
if ( ch == EOF ) goto bad_return;
if ( fgetc( fp ) != '&' ) goto bad_return;
wy = 18;
}
if ( fscanf( fp, "%f", dp ) <= 0 ) goto bad_return;
}
}
fseek( fp, filepos[curr_pattern], 0 );
return( 0 );
bad_return:
PutStr( "Bad file format\n" );
return( -1 );
}
WriteFData( flag )
int flag; /* Whether or not desired output should be written */
{
int wx, wy; /* work index */
if ( curr_pattern < 0 ) curr_pattern = 0;
if ( curr_pattern >= n_patterns ) {
curr_pattern = n_patterns;
n_patterns++;
fseek( fp, 0l, 2 ); /* Seek End of File */
filepos[curr_pattern] = ftell( fp );
}
fseek( fp, filepos[curr_pattern], 0 );
fputc( ' ', fp );
for ( wx = 0, wy = 18; wx < n_inputs; wx++, wy-- )
{
if ( wy == 0 ) {
fputc( NEW_LINE, fp );
fputc( '&' , fp ); /* Continuation line */
wy = 18;
}
fprintf( fp, " %2.0f.", fdata[wx] );
}
fputc( NEW_LINE, fp );
curr_pattern++;
}
Shuffle( size )
int size;
{
int wx, wy, wz;
int swap;
for ( wx = 0, wy = size; wx < size; wx++, wy-- ) {
wz = rand( ) % wy;
swap = shuffle_array[wx];
shuffle_array[wx] = shuffle_array[wx + wz];
shuffle_array[wx + wz] = swap;
}
}
/*****************************************************************************/
/* */
/* Functions necessary for handling the User I/O package itself. */
/* */
/*****************************************************************************/
/**/
/* UIO_Init function to initialize user I/O
************************************************************************
*/
NINT UIO_Init(file)
TEXT *file;
{
NINT ret_val = UIO_OK;
fp = (FILE *)0;
PMenuList[CR_SCROLL].text = "Scroll";
PMenuList[CR_SCROLL].text2 = "";
PMenuList[CR_SEEK].text = "Seek";
PMenuList[CR_SEEK].text2 = "";
PMenuList[CR_ENTER].text = "Enter";
PMenuList[CR_ENTER].text2 = "";
PMenuList[CR_QUIT].text = "Quit";
PMenuList[CR_QUIT].text2 = "";
PMenuList[CR_AUTO].text = "Auto";
PMenuList[CR_AUTO].text2 = "";
AIOMenu.item = PMenuList;
LrnMenu.item = PMenuList;
/* Get screen parameters */
ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry);
if ( ncolor < 8 ) {
gm_intcolor = 1;
gm_txtcolor = 0;
gm_outcolor = 0;
} else {
gm_intcolor = 7;
gm_txtcolor = 4;
gm_outcolor = 0;
}
ip_grid.gr_color[GR_OFF] = f1_grid.gr_color[GR_OFF] = gm_intcolor;
ip_grid.gr_color[GR_ON] = f1_grid.gr_color[GR_ON] = gm_txtcolor;
ip_grid.gr_color[GR_LINE] = f1_grid.gr_color[GR_LINE] = gm_txtcolor;
InitGMenu( &AIOMenu, chrx, chry );
AIOMenu.x0 = 0; /* Menu position relative to window */
AIOMenu.y0 = 0;
InitGMenu( &LrnMenu, chrx, chry );
LrnMenu.x0 = 0; /* Menu position relative to window */
LrnMenu.y0 = 0;
return(ret_val);
}
/**/
/* UIO_Term function to terminate user I/O
************************************************************************
*/
NINT UIO_Term(process)
NINT process;
{
NINT ret_val = UIO_OK;
/* if a file is open */
if ( fp != (FILE *)0 ) fclose( fp ); /* close it */
fp = (FILE *)0;
PutStr( "bye bye\n" ); /* display a message */
return(ret_val);
}
/**/
/* UIO_Attention function to signal user I/O program
************************************************************************
*/
NINT UIO_Attention()
{
NINT ret_val = UIO_OK;
NINT newfile;
NINT key, xp, yp, button; /* mouse interface */
TEXT sbuf[60], *sp; /* work string and pointer */
GMENU_ITEM *gmip;
if ( fp != (FILE *) 0 ) fclose( fp );
sprintf( sbuf,
"Enter name of pattern file ( default: %s ):", filenroot );
PutStr( sbuf );
sp = GetStr( );
PutStr("\n");
if ( strcmp( sp, "" ) != 0 ) strncpy( filenroot, sp, 9 );
strcpy( filename, filenroot );
strcat( filename, ".nna" );
if ( (fp = fopen( filename, "r" )) == (FILE *)0 )
{
newfile = 1;
}
else
{
newfile = 0;
fclose(fp);
}
if ( ( fp = fopen( filename, "a+" ) ) == (FILE *)0 ) {
sprintf( sbuf, "Cannot open file <%s>\n", filename );
PutStr( sbuf );
goto aio_return;
}
fseek( fp, 0l, 0 );
if ( newfile ) { /* New file */
PutStr( "Enter number of pixels across:" );
sp = GetStr( );
PutStr("\n");
sscanf( sp, "%ld", &n_across );
if ( n_across < N_ACR_MIN ) n_across = N_ACR_MIN;
if ( n_across > N_ACR_MAX ) n_across = N_ACR_MAX;
PutStr( "Enter number of pixels down:" );
sp = GetStr( );
PutStr("\n");
sscanf( sp, "%ld", &n_down );
if ( n_down < N_DWN_MIN ) n_down = N_DWN_MIN;
if ( n_down > N_DWN_MAX ) n_down = N_DWN_MAX;
fprintf( fp,
"! number of pixels across = %2ld, number of pixels down = %2ld%s",
n_across, n_down, NEW_LINE_STR );
} else {
if ( fgets( fbuf, MAX_STR, fp ) == 0 ) goto aio_read_error;
sscanf( fbuf,
"! number of pixels across = %ld, number of pixels down = %ld",
&n_across, &n_down );
if ( n_across < N_ACR_MIN || n_across > N_ACR_MAX ||
n_down < N_DWN_MIN || n_down > N_DWN_MAX ) {
PutStr( "Unexpected values in file\n" );
goto aio_return;
}
}
n_inputs = n_across * n_down; /* # PEs in input buffer */
n_patterns = FillPosArr( ); /* Fill Position array */
curr_pattern = n_patterns; /* End of file */
ReadFData( 0 ); /* Special eof case */
/* Set up grid window and grid */
ip_grid.gr_data = fdata;
ip_grid.gr_key = 1;
ip_grid.gr_flag = 0;
ip_grid.gr_xoff = ip_grid.gr_yoff = 0;
ip_grid.gr_nx = n_across;
ip_grid.gr_ny = n_down;
ip_grid.gr_pxx = 16 - ( n_across + n_down ) / 4;
ip_grid.gr_pxy = ip_grid.gr_pxx;
wxstrt[0] = ( xsize - (ip_grid.gr_nx * (ip_grid.gr_pxx+1) + 1) ) / 2 ;
wystrt[0] = ( ysize - (ip_grid.gr_ny * (ip_grid.gr_pxy+1) + 1) ) / 2 ;
wxend[0] = wxstrt[0] + ip_grid.gr_nx * (ip_grid.gr_pxx+1);
wyend[0] = wystrt[0] + ip_grid.gr_ny * (ip_grid.gr_pxy+1);
ug_window( ip_grid.gr_key, ip_grid.gr_color[GR_OFF],
wxstrt[0], wystrt[0], wxend[0], wyend[0] );
DispGrid( &ip_grid );
DispPattern( &ip_grid );
/* Set up menu and menu window */
AIOMenu.key = 2;
wxstrt[1] = ( xsize - AIOMenu.x1 ) / 2;
wxend[1] = wxstrt[1] + AIOMenu.x1 + 2;
wystrt[1] = ( wystrt[0] - AIOMenu.y1 ) / 2;
wyend[1] = wystrt[1] + AIOMenu.y1 + 2;
ug_window( AIOMenu.key, ip_grid.gr_color[GR_OFF],
wxstrt[1], wystrt[1], wxend[1], wyend[1] );
DispGMenu( &AIOMenu );
for ( ; ; ) {
while ( ( gmip=LookGMenu(&AIOMenu,&button) ) != (GMENU_ITEM *) 0 ) {
switch( gmip->code ) {
case CR_SCROLL:
if ( button == MBUT_LEFT ) { /* Scroll down */
curr_pattern++;
if ( ReadFData( 0 ) < 0 ) goto aio_return;
DispPattern( &ip_grid );
}
if ( button == MBUT_RIGHT ) { /* Scroll up */
curr_pattern--;
if ( ReadFData( 0 ) < 0 ) goto aio_return;
DispPattern( &ip_grid );
}
break;
case CR_SEEK:
if ( button == MBUT_LEFT ) { /* Start of file data */
curr_pattern = 0;
if ( ReadFData( 0 ) < 0 ) goto aio_return;
DispPattern( &ip_grid );
}
if ( button == MBUT_RIGHT ) { /* End of file data */
curr_pattern = n_patterns;
if ( ReadFData( 0 ) < 0 ) goto aio_return;
DispPattern( &ip_grid );
}
break;
case CR_ENTER:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
WriteFData( 0 );
ReadFData( 0 );
DispPattern( &ip_grid );
}
break;
case CR_QUIT:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
ret_val = UIO_ERROR;
goto aio_return;
}
break;
}
/* If button was pressed, wait for it to be released */
while ( button != 0 )
ug_mouse( &key, &xp, &yp, &button );
}
Sketch( &ip_grid );
}
aio_read_error:
sprintf( sbuf, "Error reading file <%s>\n", filename );
PutStr( sbuf );
aio_return:
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
ug_windel( 1 );
ug_windel( 2 );
return(ret_val);
}
/**/
/*****************************************************************************/
/* */
/* Functions necessary for handling a learning session. */
/* */
/*****************************************************************************/
/* UIO_Learn_Start function to initiate learning
************************************************************************
*/
NINT UIO_Learn_Start()
{
NINT ret_val = UIO_OK;
NINT wx, wy; /* work indices */
TEXT sbuf[60], *sp; /* work string and pointer */
abortio = 0; /* abort flag */
autoip = 0; /* Assume hand input */
wrstepf = 0; /* write step flag */
if ( fp == (FILE *)0 ) { /* Not yet opened */
sprintf( sbuf,
"Enter name of pattern file ( default: %s ):", filenroot );
PutStr( sbuf );
sp = GetStr( );
PutStr("\n");
if ( strcmp( sp, "" ) != 0 ) strncpy( filenroot, sp, 9 );
strcpy( filename, filenroot );
strcat( filename, ".nna" );
if ( ( fp = fopen( filename, "r" ) ) == (FILE *)0 ) {
sprintf( sbuf, "Cannot open file <%s>; learn aborted\n", filename );
PutStr( sbuf );
abortio = 1;
goto lrns_return;
}
shufflex = 0;
/* Initialize shuffle array */
for ( wx = 0; wx < MAX_N_PATTERNS; wx++ )
shuffle_array[ wx ] = wx;
n_patterns = FillPosArr( ); /* Fill Position array */
curr_pattern = 0; /* Start of of file */
}
fseek( fp, 0l, 0 ); /* rewind */
if ( fgets( fbuf, MAX_STR, fp ) == 0 ) {
PutStr( "Read error; learn aborted\n" );
abortio = 1;
goto lrns_return;
}
sscanf( fbuf,
"! number of pixels across = %ld, number of pixels down = %ld",
&n_across, &n_down );
if ( n_across < N_ACR_MIN || n_across > N_ACR_MAX ||
n_down < N_DWN_MIN || n_down > N_DWN_MAX ) {
PutStr( "Unexpected values in file; learn aborted\n" );
abortio = 1;
goto lrns_return;
}
n_inputs = n_across * n_down; /* # PEs in input buffer */
ug_rdnetinf( &nlayp, &ninp, &noutp, <ype, &csp, &netnp );
if ( n_inputs != ninp ) {
PutStr( "Data incompatible with network; learn aborted\n" );
abortio = 1;
goto lrns_return;
}
/* Initialize grids and associated window */
ip_grid.gr_data = fdata;
f1_grid.gr_data = f1data;
ip_grid.gr_key = f1_grid.gr_key = 1;
ip_grid.gr_flag = f1_grid.gr_flag = 0;
ip_grid.gr_nx = f1_grid.gr_nx = n_across;
ip_grid.gr_ny = f1_grid.gr_ny =n_down;
ip_grid.gr_pxx = f1_grid.gr_pxx = 16 - ( n_across / 2 );
ip_grid.gr_pxy = f1_grid.gr_pxy = ip_grid.gr_pxx;
ip_grid.gr_xoff = 3 * chrx;
ip_grid.gr_yoff = f1_grid.gr_yoff = 4 * chry;
f1_grid.gr_xoff = ip_grid.gr_xoff + 22 * chrx;
wx = 50 * chrx;
wy = 5 * chry + ip_grid.gr_ny * ( ip_grid.gr_pxy + 1 ) + 1;
wxstrt[0] = ( xsize - wx ) / 2 ;
wystrt[0] = ( ysize - wy ) / 2 ;
wxend[0] = wxstrt[0] + wx;
wyend[0] = wystrt[0] + wy;
ug_window( ip_grid.gr_key, ip_grid.gr_color[GR_OFF],
wxstrt[0], wystrt[0], wxend[0], wyend[0] );
/* Set up menu and menu window */
LrnMenu.key = 2;
wxstrt[1] = ( xsize - LrnMenu.x1 ) / 2;
wxend[1] = wxstrt[1] + LrnMenu.x1 + 2;
wystrt[1] = ( wystrt[0] - LrnMenu.y1 ) / 2;
wyend[1] = wystrt[1] + LrnMenu.y1 + 2;
ug_window( LrnMenu.key, gm_intcolor,
wxstrt[1], wystrt[1], wxend[1], wyend[1] );
lrns_return:
if ( abortio ) {
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
ug_windel( 1 );
ug_windel( 2 );
ret_val = UIO_ERROR;
}
return(ret_val);
}
/**/
/* UIO_Learn_Input function to read training data
************************************************************************
*/
NINT UIO_Learn_Input(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
NINT key, xp, yp, button; /* mouse interface */
TEXT sbuf[60], *sp; /* work string and pointer */
GMENU_ITEM *gmip;
if ( abortio ) goto lrnin_return;
/* Display grids */
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
ug_winclr( ip_grid.gr_key );
DispGrid( &ip_grid );
DispGrid( &f1_grid );
ug_puts( ip_grid.gr_key, ip_grid.gr_color[GR_ON], 0,
3 * chrx, 3 * chry, ip_text, 0 );
ug_puts( ip_grid.gr_key, ip_grid.gr_color[GR_ON], 0,
25 * chrx, 3 * chry, f1_text, 0 );
if ( !autoip ) {
ug_winclr( LrnMenu.key );
DispGMenu( &LrnMenu );
DispPattern( &ip_grid );
for ( ; ; ) {
while ( ( gmip=LookGMenu(&LrnMenu,&button) ) != (GMENU_ITEM *) 0 ) {
switch( gmip->code ) {
case CR_SCROLL:
if ( button == MBUT_LEFT ) { /* Scroll down */
curr_pattern++;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
DispPattern( &ip_grid );
}
if ( button == MBUT_RIGHT ) { /* Scroll up */
curr_pattern--;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
DispPattern( &ip_grid );
}
break;
case CR_SEEK:
if ( button == MBUT_LEFT ) { /* Start of file data */
curr_pattern = 0;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
DispPattern( &ip_grid );
}
if ( button == MBUT_RIGHT ) { /* End of file data */
curr_pattern = n_patterns;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
DispPattern( &ip_grid );
}
break;
case CR_ENTER:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
/* added size_t cast on 3rd memcpy() arg for MS-C 7.0; 4-28-93 mjs */
#ifdef MSC
memcpy( Datap, fdata, (size_t)(n_inputs * sizeof( float )) );
#else
memcpy( Datap,
fdata, n_inputs * sizeof( float ) );
#endif
curr_pattern++;
goto lrnin_return;
}
break;
case CR_QUIT:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
ret_val = UIO_ERROR;
goto lrnin_return;
}
break;
case CR_AUTO:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
autoip = 1;
goto auto_lrn;
}
break;
}
/* If button was pressed, wait for it to be released */
while ( button != 0 )
ug_mouse( &key, &xp, &yp, &button );
}
Sketch( &ip_grid );
}
} else {
auto_lrn:
if ( shufflex == 0 ) Shuffle( n_patterns );
curr_pattern = shuffle_array[ shufflex++ ];
if ( ReadFData( 0 ) < 0 ) {
PutStr( "Error reading file; learn aborted\n" );
abortio = 1;
goto lrnin_return;
}
if ( shufflex >= n_patterns ) shufflex = 0;
/* added size_t cast on 3rd memcpy() arg for MS-C 7.0; 4-28-93 mjs */
#ifdef MSC
memcpy( Datap, fdata, (size_t)(n_inputs * sizeof( float )) );
#else
memcpy( Datap,
fdata, n_inputs * sizeof( float ) );
#endif
}
lrnin_return:
if ( abortio ) {
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
ug_windel( 1 );
ug_windel( 2 );
ret_val = UIO_ERROR;
}
return(ret_val);
}
/* FUNCTION */
NINT UIO_Learn_Output(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
/* IODATA points to an empty array of IOCOUNT values. The
elements of the array will become the desired outputs for
training purposes. These desired outputs correspond to
the most recent "RQ_LEARNIN" request.
*/
return(ret_val);
}
/**/
/* UIO_Learn_Result function to write results from learning
************************************************************************
*/
NINT UIO_Learn_Result(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
NINT key, xp, yp, button; /* mouse interface */
if ( abortio ) goto write_return;
if ( autoip ) {
} else {
for ( ; ; ) {
ug_mouse( &key, &xp, &yp, &button ); /* wait for mouse */
if (button == MBUT_LEFT || button == MBUT_RIGHT) break;
}
}
write_return:
if ( abortio ) {
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
ug_windel( 1 );
ug_windel( 2 );
ret_val = UIO_ERROR;
}
return(ret_val);
}
/* FUNCTION */
NINT UIO_Learn_End()
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}
/**/
/*****************************************************************************/
/* */
/* Functions necessary for handling a recall or testing session. */
/* */
/*****************************************************************************/
/* FUNCTION */
NINT UIO_Recall_Start()
{
NINT ret_val = UIO_OK;
NINT key, xp, yp, button; /* mouse interface */
NINT wx, wy; /* work indices */
TEXT sbuf[60], *sp; /* work string and pointer */
GMENU_ITEM *gmip;
abortio = 0; /* abort flag */
autoip = 0; /* Assume hand input */
wrstepf = 0; /* write step flag */
if ( fp == (FILE *)0 ) { /* Not yet opened */
sprintf( sbuf,
"Enter name of pattern file ( default: %s ):", filenroot );
PutStr( sbuf );
sp = GetStr( );
PutStr("\n");
if ( strcmp( sp, "" ) != 0 ) strncpy( filenroot, sp, 9 );
strcpy( filename, filenroot );
strcat( filename, ".nna" );
if ( ( fp = fopen( filename, "r" ) ) == (FILE *)0 ) {
sprintf( sbuf, "Cannot open file <%s>; learn aborted\n", filename );
PutStr( sbuf );
abortio = 1;
goto lrns_return;
}
shufflex = 0;
/* Initialize shuffle array */
for ( wx = 0; wx < MAX_N_PATTERNS; wx++ )
shuffle_array[ wx ] = wx;
n_patterns = FillPosArr( ); /* Fill Position array */
curr_pattern = 0; /* Start of of file */
}
fseek( fp, 0l, 0 ); /* rewind */
if ( fgets( fbuf, MAX_STR, fp ) == 0 ) {
PutStr( "Read error; learn aborted\n" );
abortio = 1;
goto lrns_return;
}
sscanf( fbuf,
"! number of pixels across = %ld, number of pixels down = %ld",
&n_across, &n_down );
if ( n_across < N_ACR_MIN || n_across > N_ACR_MAX ||
n_down < N_DWN_MIN || n_down > N_DWN_MAX ) {
PutStr( "Unexpected values in file; learn aborted\n" );
abortio = 1;
goto lrns_return;
}
n_inputs = n_across * n_down; /* # PEs in input buffer */
ug_rdnetinf( &nlayp, &ninp, &noutp, <ype, &csp, &netnp );
if ( n_inputs != ninp ) {
PutStr( "Data incompatible with network; learn aborted\n" );
abortio = 1;
goto lrns_return;
}
/* Initialize grids and associated window */
ip_grid.gr_data = fdata;
f1_grid.gr_data = f1data;
ip_grid.gr_key = f1_grid.gr_key = 1;
ip_grid.gr_flag = f1_grid.gr_flag = 0;
ip_grid.gr_nx = f1_grid.gr_nx = n_across;
ip_grid.gr_ny = f1_grid.gr_ny =n_down;
ip_grid.gr_pxx = f1_grid.gr_pxx = 16 - ( n_across / 2 );
ip_grid.gr_pxy = f1_grid.gr_pxy = ip_grid.gr_pxx;
ip_grid.gr_xoff = 3 * chrx;
ip_grid.gr_yoff = f1_grid.gr_yoff = 4 * chry;
f1_grid.gr_xoff = ip_grid.gr_xoff + 22 * chrx;
wx = 50 * chrx;
wy = 5 * chry + ip_grid.gr_ny * ( ip_grid.gr_pxy + 1 ) + 1;
wxstrt[0] = ( xsize - wx ) / 2 ;
wystrt[0] = ( ysize - wy ) / 2 ;
wxend[0] = wxstrt[0] + wx;
wyend[0] = wystrt[0] + wy;
ug_window( ip_grid.gr_key, ip_grid.gr_color[GR_OFF],
wxstrt[0], wystrt[0], wxend[0], wyend[0] );
/* Set up menu and menu window */
LrnMenu.key = 2;
wxstrt[1] = ( xsize - LrnMenu.x1 ) / 2;
wxend[1] = wxstrt[1] + LrnMenu.x1 + 2;
wystrt[1] = ( wystrt[0] - LrnMenu.y1 ) / 2;
wyend[1] = wystrt[1] + LrnMenu.y1 + 2;
ug_window( LrnMenu.key, gm_intcolor,
wxstrt[1], wystrt[1], wxend[1], wyend[1] );
lrns_return:
if ( abortio ) {
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
ug_windel( 1 );
ug_windel( 2 );
ret_val = UIO_ERROR;
}
return(ret_val);
}
/* FUNCTION */
NINT UIO_Read(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
NINT key, xp, yp, button; /* mouse interface */
GMENU_ITEM *gmip;
if ( abortio ) goto lrnin_return;
/* Display grids */
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
ug_winclr( ip_grid.gr_key );
DispGrid( &ip_grid );
DispGrid( &f1_grid );
ug_puts( ip_grid.gr_key, ip_grid.gr_color[GR_ON], 0,
3 * chrx, 3 * chry, ip_text, 0 );
ug_puts( ip_grid.gr_key, ip_grid.gr_color[GR_ON], 0,
25 * chrx, 3 * chry, f1_text, 0 );
if ( !autoip ) {
ug_winclr( LrnMenu.key );
DispGMenu( &LrnMenu );
DispPattern( &ip_grid );
for ( ; ; ) {
while ( ( gmip=LookGMenu(&LrnMenu,&button) ) != (GMENU_ITEM *) 0 ) {
switch( gmip->code ) {
case CR_SCROLL:
if ( button == MBUT_LEFT ) { /* Scroll down */
curr_pattern++;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
DispPattern( &ip_grid );
}
if ( button == MBUT_RIGHT ) { /* Scroll up */
curr_pattern--;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
DispPattern( &ip_grid );
}
break;
case CR_SEEK:
if ( button == MBUT_LEFT ) { /* Start of file data */
curr_pattern = 0;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
DispPattern( &ip_grid );
}
if ( button == MBUT_RIGHT ) { /* End of file data */
curr_pattern = n_patterns;
if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
DispPattern( &ip_grid );
}
break;
case CR_ENTER:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
/* added size_t cast on 3rd memcpy() arg for MS-C 7.0; 4-28-93 mjs */
#ifdef MSC
memcpy( Datap, fdata, (size_t)(n_inputs * sizeof( float )) );
#else
memcpy( Datap,
fdata, n_inputs * sizeof( float ) );
#endif
curr_pattern++;
goto lrnin_return;
}
break;
case CR_QUIT:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
ret_val = UIO_ERROR;
goto lrnin_return;
}
break;
case CR_AUTO:
if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
autoip = 1;
goto auto_lrn;
}
break;
}
/* If button was pressed, wait for it to be released */
while ( button != 0 )
ug_mouse( &key, &xp, &yp, &button );
}
Sketch( &ip_grid );
}
} else {
auto_lrn:
if ( shufflex == 0 ) Shuffle( n_patterns );
curr_pattern = shuffle_array[ shufflex++ ];
if ( ReadFData( 0 ) < 0 ) {
PutStr( "Error reading file; learn aborted\n" );
abortio = 1;
goto lrnin_return;
}
if ( shufflex >= n_patterns ) shufflex = 0;
/* added size_t cast on 3rd memcpy() arg for MS-C 7.0; 4-28-93 mjs */
#ifdef MSC
memcpy( Datap, fdata, (size_t)(n_inputs * sizeof( float )) );
#else
memcpy( Datap,
fdata, n_inputs * sizeof( float ) );
#endif
}
lrnin_return:
if ( abortio ) {
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
ug_windel( 1 );
ug_windel( 2 );
ret_val = UIO_ERROR;
}
return(ret_val);
}
/**/
/* UIO_Write function to write out results
************************************************************************
*/
NINT UIO_Write(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
NINT key, xp, yp, button; /* mouse interface */
if ( abortio ) goto write_return;
if ( autoip ) {
} else {
for ( ; ; ) {
ug_mouse( &key, &xp, &yp, &button ); /* wait for mouse */
if (button == MBUT_LEFT || button == MBUT_RIGHT) break;
}
}
write_return:
if ( abortio ) {
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
ug_windel( 1 );
ug_windel( 2 );
ret_val = UIO_ERROR;
}
return(ret_val);
}
/**/
/* UIO_Write_Step function to write interim results
************************************************************************
*/
NINT UIO_Write_Step(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
/* Alternates between pre-resonant and post-resonant F1 activity */
if ( abortio ) goto wrstep_return;
if ( wrstepf ) {
/* added size_t cast on 3rd memcpy() arg for MS-C 7.0; 4-28-93 mjs */
#ifdef MSC
memcpy( f1_grid.gr_data, Datap, (size_t)(n_inputs * sizeof( float )) );
#else
memcpy( f1_grid.gr_data, Datap,
n_inputs * sizeof( float ) );
#endif
DispPattern( &f1_grid );
wrstepf = 0;
} else {
/* added size_t cast on 3rd memcpy() arg for MS-C 7.0; 4-28-93 mjs */
#ifdef MSC
memcpy( f1_grid.gr_data, Datap, (size_t)(n_inputs * sizeof( float )) );
#else
memcpy( f1_grid.gr_data, Datap,
n_inputs * sizeof( float ) );
#endif
DispPattern( &ip_grid );
wrstepf = 1;
}
wrstep_return:
if ( abortio ) {
if ( fp != (FILE *)0 ) fclose( fp );
fp = (FILE *)0;
ug_windel( 1 );
ug_windel( 2 );
ret_val = UIO_ERROR;
}
return(ret_val);
}
/**/
/* UIO_Recall_Test function to read desired output for recall test
************************************************************************
*/
NINT UIO_Recall_Test(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
/* IODATA points to an empty array of IOCOUNT values. The
elements of the array will become the desired outputs for
recall purposes. This request is only made during a
Execute Network/Recall Test.
*/
return(ret_val);
}
/**/
/* UIO_Recall_End
************************************************************************
*/
NINT UIO_Recall_End()
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}
/**/
/*****************************************************************************/
/* */
/* Other miscelaneous functions. */
/* */
/*****************************************************************************/
/* FUNCTION */
NINT UIO_Instrument(Instrument_id, nDataElems, DataElemp)
NINT Instrument_id;
NINT nDataElems;
SREAL *DataElemp;
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}
/* FUNCTION */
NINT UIO_ObjFunc(eoeflag, DataElemp)
NINT eoeflag;
SREAL *DataElemp;
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}
/* FUNCTION */
NINT UIO_Rewind()
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
if ( fp != (FILE *)0 ) fseek( fp, 0l, 0 );
return(ret_val);
}
/**/
/* FUNCTION */
NINT UIO_Explain(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}